package com.zt.ztzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zt.ztzx.model.dto.system.SysRoleDto;
import com.zt.ztzx.model.entity.system.SysRole;
import com.zt.ztzx.model.vo.common.Result;
import com.zt.ztzx.service.SysRoleService;
import com.zt.ztzx.mapper.SysRoleMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 86186
* @description 针对表【sys_role(角色)】的数据库操作Service实现
* @createDate 2024-08-20 14:21:16
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Override
    public Result<Page<SysRole>> getPageData(SysRoleDto sysRoleDto, Integer pageNum, Integer pageSize) {
        if(StringUtils.checkValNull(pageNum)||StringUtils.checkValNull(pageSize))return Result.build(null,400,"参数不能为空");
        Page<SysRole> page = new Page<>(pageNum, pageSize);
        this.page(page, Wrappers.<SysRole>lambdaQuery().like(StringUtils.checkValNotNull(sysRoleDto.getRoleName()),SysRole::getRoleName,sysRoleDto.getRoleName()));
        for (SysRole record : page.getRecords()) {
            System.err.println(record);
        }
        return Result.build(page,200,"查询成功");
    }

    @Override
    public Result add(SysRole sysRole) {
        if(Objects.isNull(sysRole))return Result.build(null,400,"参数不能为空");
        if(this.save(sysRole))return Result.build(null,200,"添加成功");
        return Result.build(null,400,"添加失败");
    }

    @Override
    public Result removeData(Long id) {
        System.out.println(id);
        boolean flag = this.removeById(id);
        return flag?Result.build(null,200,"删除成功"):Result.build(null,400,"删除失败");
    }

    @Override
    public Result updateData(SysRole sysRole) {
        if (Objects.isNull(sysRole))return Result.build(null,400,"参数不能为空");
        if(this.updateById(sysRole))return Result.build(null,200,"修改成功");
        return Result.build(null,400,"修改失败");
    }

    /**
     * 获取所有角色
     * @return
     */
    @Override
    public Result<List<SysRole>> findAll() {
        List<SysRole> list = this.list();
        return Result.build(list,200,"查询成功");
    }
}




